package net.csdn.business.gateway.config;

import net.csdn.business.common.oauth2.component.*;
import net.csdn.business.common.oauth2.service.IOauthAccessTokenService;
import net.csdn.business.common.oauth2.service.IOauthClientInfoService;
import net.csdn.business.common.oauth2.service.IOauthRefreshTokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.oauth2.provider.token.ResourceServerTokenServices;

/**
 * @Auther: zhangyalei
 * @Date: 2022/12/5 23:12
 * @Description: 对openatom-common-oauth2模块中的component下的类实例化
 */
@Configuration
// @Import({ThreadPoolConfig.class})
public class CustomOath2Config {
    @Autowired
    private IOauthAccessTokenService oauthAccessTokenService;

    @Autowired
    private IOauthRefreshTokenService oauthRefreshTokenService;

    @Autowired
    private IOauthClientInfoService oauthClientInfoService;

    @Autowired
    private RedisConnectionFactory connectionFactory;


    @Bean
    CustomJdbcTokenStore getCustomJdbcTokenStore() {
        return new CustomJdbcTokenStore(oauthAccessTokenService, oauthRefreshTokenService);
    }

    @Bean
    OauthAsyncService getOauthAsyncService(CustomJdbcTokenStore customJdbcTokenStore, ThreadPoolTaskExecutor taskExecutor) {
        return new OauthAsyncService(customJdbcTokenStore, taskExecutor);
    }

    @Bean
    @Primary
    CustomRedisTokenStore getCustomRedisTokenStore(CustomJdbcTokenStore customJdbcTokenStore, OauthAsyncService oauthAsyncService) {
        return new CustomRedisTokenStore(connectionFactory, customJdbcTokenStore, oauthAsyncService);
    }


    @Bean
    @Primary
    ClientDetailsService getCustomClientDetailsService() {
        return new CustomClientDetailsService(oauthClientInfoService);
    }

    @Bean
    @Primary
    ResourceServerTokenServices getResourceServerTokenServices(CustomRedisTokenStore customRedisTokenStore) {
        return new CloudLocalResourceServerTokenServices(customRedisTokenStore);
    }
}